package com.aygxy.carproject.service.impl;

import com.aygxy.carproject.ex.ServiceCode;
import com.aygxy.carproject.ex.ServiceException;
import com.aygxy.carproject.mapper.ProductSkuMapper;
import com.aygxy.carproject.pojo.product.dto.SkuAddNewDTO;
import com.aygxy.carproject.pojo.product.dto.SkuUpdateFullInfoDTO;
import com.aygxy.carproject.pojo.product.entity.Sku;
import com.aygxy.carproject.pojo.product.vo.SkuStandardVO;
import com.aygxy.carproject.service.ProductSkuService;
import com.aygxy.carproject.utils.IdGeneratorUtils;
import com.aygxy.carproject.utils.ListConvertUtils;
import com.aygxy.carproject.web.JsonPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ProductSkuServiceImpl implements ProductSkuService {
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Override
    public int reduceStockNum(Long skuId, Integer quantity) {
        return productSkuMapper.updateStockById(skuId,quantity);
    }

    @Override
    public SkuStandardVO getById(Long id) {
        SkuStandardVO sku = productSkuMapper.getById(id);
        if (sku == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "获取SKU详情失败，尝试访问的数据不存在！");
        }
        return sku;
    }

    @Override
    public JsonPage<SkuStandardVO> list(Long spuId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SkuStandardVO> skus = productSkuMapper.listBySpuId(spuId);
        return JsonPage.restPage(new PageInfo<>(skus));
    }


    @Override
    public void addNew(SkuAddNewDTO skuAddNewDTO) {
        log.debug("skuAddNewDTO = {}", skuAddNewDTO);
       // Long skuId = IdGeneratorUtils.getDistributeId(IdGeneratorUtils.Key.SKU);

        Sku sku = new Sku();
        BeanUtils.copyProperties(skuAddNewDTO, sku);
        productSkuMapper.insert(sku);

//        String specifications = skuAddNewDTO.getSpecifications();
//        log.debug("specifications = {}", specifications);
//        List<SkuSpecification> skuSpecificationList = ListConvertUtils.stringToList(specifications, SkuSpecification.class);
//        if (skuSpecificationList != null) {
//            for (SkuSpecification skuSpecification : skuSpecificationList) {
//                log.debug("skuSpecification = {}", skuSpecification);
//                skuSpecification.setSkuId(skuId);
//            }
//        }
//        skuSpecificationMapper.insertBatch(skuSpecificationList);
    }

    @Override
    public void  updateFullInfoById(Long id, SkuUpdateFullInfoDTO skuUpdateFullInfoDTO) {
        Object checkExistQueryResult = productSkuMapper.getById(id);
        if (checkExistQueryResult == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "更新SKU失败，尝试访问的数据不存在！");
        }

        Sku sku = new Sku();
        sku.setId(id);
        BeanUtils.copyProperties(skuUpdateFullInfoDTO, sku);
        int rows = productSkuMapper.updateFullInfoById(sku);
        if (rows != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "更新SKU失败，服务器忙，请稍后再次尝试！");
        }
    }
}
